package com.key.win.business.chat.ctrl;

import com.key.win.base.auth.AuthenticationUtil;
import com.key.win.base.exception.BizException;
import com.key.win.base.web.PageRequest;
import com.key.win.base.web.PageResult;
import com.key.win.base.web.Result;
import com.key.win.business.chat.model.ImGroup;
import com.key.win.business.chat.model.ImGroupNoticeRead;
import com.key.win.business.chat.service.ImGroupNoticeReadService;
import com.key.win.business.chat.service.ImGroupService;
import com.key.win.common.log.annotation.LogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/im/group/*")
@Api("好友相关的api")
public class ImGroupCtrl {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ImGroupService imGroupService;

    @Autowired
    private ImGroupNoticeReadService imGroupNoticeReadService;

    @PostMapping("/findGroupByPaged")
    @ApiOperation(value = "Group分页")
    @LogAnnotation(module = "im", recordRequestParam = false)
    public PageResult<ImGroup> findGroupByPaged(@RequestBody PageRequest<ImGroup> pageRequest) {
        return imGroupService.findGroupByPaged(pageRequest);
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "获取Group")
    @LogAnnotation(module = "im", recordRequestParam = false)
    public Result get(@PathVariable String id) {
        return Result.succeed(imGroupService.getById(id));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    @LogAnnotation(module = "im", recordRequestParam = true)
    public Result delete(@PathVariable String id) {
        boolean b = imGroupService.deleteGroup(id);
        return Result.result(b);
    }

    @PostMapping("/addGroup")
    @ApiOperation(value = "创建组")
    @LogAnnotation(module = "im", recordRequestParam = true)
    public Result addGroup(@RequestBody ImGroup imGroup) {
        imGroup.setUserId(AuthenticationUtil.getUserId());
        boolean b = imGroupService.addGroup(imGroup);
        return Result.result(b);
    }

    @PostMapping("/updateGroup")
    @ApiOperation(value = "更新组")
    @LogAnnotation(module = "im", recordRequestParam = true)
    public Result updateGroup(@RequestBody ImGroup imGroup) {
        boolean b = imGroupService.updateGroup(imGroup);
        return Result.result(b);
    }

    @GetMapping("/findGroup/{userId}")
    @ApiOperation(value = "根据用户获取Group")
    @LogAnnotation(module = "im", recordRequestParam = false)
    public Result getGroup(@PathVariable String userId) {
        return Result.succeed(imGroupService.findGroupByUserId(userId));
    }

    @GetMapping("/findGroup/cu")
    @ApiOperation(value = "获取当前用户的Group")
    @LogAnnotation(module = "im", recordRequestParam = false)
    public Result getGroupByCurrentUser() {
        return Result.succeed(imGroupService.findGroupByUserId(AuthenticationUtil.getUserId()));
    }

    @GetMapping("/findGroupNoticeStatus/{groupId}")
    @ApiOperation(value = "获取当前用户是否已经阅读公告:[true:没阅读,false:已阅读]")
    @LogAnnotation(module = "im", recordRequestParam = false)
    public Result findGroupNoticeStatusByCurrentUser(@PathVariable String groupId) {
        ImGroupNoticeRead imGroupNoticeRead = buildImGroupNoticeRead(groupId);
        List<ImGroupNoticeRead> imGroupNoticeReads = imGroupNoticeReadService.findImGroupNoticeRead(imGroupNoticeRead);
        if (CollectionUtils.isEmpty(imGroupNoticeReads)) {
            return Result.succeed(true);
        }
        return Result.succeed(false);
    }

    @GetMapping("/addGroupNoticeRead/{groupId}")
    @ApiOperation(value = "公告已经阅读添加接口")
    @LogAnnotation(module = "im", recordRequestParam = false)
    public Result addGroupNoticeRead(@PathVariable String groupId) {
        ImGroupNoticeRead imGroupNoticeRead = buildImGroupNoticeRead(groupId);
        return Result.result(imGroupNoticeReadService.saveOrUpdateImGroupNoticeRead(imGroupNoticeRead));
    }

    private ImGroupNoticeRead buildImGroupNoticeRead(@PathVariable String groupId) {
        ImGroupNoticeRead imGroupNoticeRead = new ImGroupNoticeRead();
        imGroupNoticeRead.setUserId(AuthenticationUtil.getUserId());
        ImGroup byId = imGroupService.getById(groupId);
        if (byId == null) {
            logger.error("当前组信息[{0}]不存在", groupId);
            throw new BizException("当前组信息不存在！");
        }
        imGroupNoticeRead.setNoticeId(byId.getNoticeId());
        imGroupNoticeRead.setGroupId(groupId);
        return imGroupNoticeRead;
    }
}
